import { message } from 'antd';
import { formatMessage } from 'umi/locale';
export default {
  namespace: 'lc',
  state: {
    calImageSrc: null, // 标定用的图片
    startModalVisble: false, // 启动提示框是否显示
    startInfo: '', //设备启动提示框的内容
    calibrationError: [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })], //标定的误差信息
    calibrationMtx: [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })], // 标定的矩阵
    calibrationOffsetUsed: [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })], // 标定的时候使用的激光偏移值
    calibarionRobotUsed: [
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
      [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
    ],
  },
  effects: {
    *handleLineCamCalMsg({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val == 'updateLineCamCalImage') {
        yield put({
          type: 'updateLineCamCalImage',
          payload: msg.calImageSrc,
        });
      } else if (msg.val == 'updateStartState') {
        yield put({
          type: 'updateStartState',
          payload: [msg.startModalVisble, msg.startInfo],
        });
      } else if (msg.val == 'updateCalibrationMtx') {
        yield put({
          type: 'updateCalibrationMtx',
          payload: [
            msg.calibrationError,
            msg.calibrationMtx,
            msg.calibrationOffsetUsed,
            msg.calibarionRobotUsed,
          ],
        });
      } else if (msg.val == 'updateCheckSizeImageSrc') {
      }
    },
    *calcTransMtxNinePoints({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'lineCamCal',
        'web_request_calc_trans_mtx_nine_points',
        [payload]
      );
      if (response === 'ok') {
        message.success('标定成功!', 3);
      } else {
        message.error('标定失败' + response, 5);
      }
    },

    *openLineCamCalImageDir({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'lineCamCal',
        'web_request_open_line_cam_cal_image_dir',
        []
      );
      if (response !== 'ok') {
        message.error('打开失败,' + response, 5);
      }
    },
    *clickStopDevice({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterZmc',
        'web_request_cutter_zmc_stop',
        []
      );
      if (response !== 'ok') {
        message.error('停止指令下发失败,' + response, 5);
      } else {
        message.success('停止指令下发成功,', 5);
      }
    },
    *moveToPoint({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'lineCamCal', 'web_request_move_to_point', [
        payload,
      ]);
      if (response !== 'ok') {
        message.error('移动失败,' + response, 5);
      }
    },

    *verifyTransMtx({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'lineCamCal', 'web_request_verify_trans_mtx', [
        payload,
      ]);
      if (response !== 'ok') {
        message.error('验证失败,' + response, 5);
      }
    },

    *clickStartDevice({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'lineCamCal', 'web_request_start_device', []);
      if (response === 'ok') {
        message.success('设备启动成功!');
      } else {
        message.error('设备启动失败,' + response, 5);
      }
    },
    *getImageForLineCamCal({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'lineCamCal',
        'web_request_get_image_for_line_cam_cal',
        []
      );
      if (response === 'ok') {
        message.success('图片获取成功!');
      } else {
        message.error('图片获取失败' + response, 5);
      }
    },
    *calcTransMtx({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'lineCamCal', 'web_request_calc_trans_mtx', [
        payload,
      ]);
      if (response === 'ok') {
        message.success('标定成功!', 3);
      } else {
        message.error('标定失败' + response, 5);
      }
    },
    *saveTransMtx({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'lineCamCal',
        'web_request_save_trans_mtx',
        []
      );
      if (response === 'ok') {
        message.success('保存成功', 3);
      } else {
        message.error('保存失败' + response, 5);
      }
    },
    *closeCamDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'camZmc', 'close_door_alarm');
      if (response === 0) {
        message.success('关闭上料门报警成功', 3);
      } else {
        message.error('关闭上料门报警失败!' + response, 5);
      }
    },
    *openCamDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'camZmc', 'open_door_alarm');
      if (response === 0) {
        message.success('打开上料门报警成功', 3);
      } else {
        message.error('打开上料门报警失败!' + response, 5);
      }
    },
  },
  reducers: {
    updateLineCamCalImage(state, action) {
      return {
        ...state,
        calImageSrc: 'data:image/jpeg;base64,' + action.payload,
      };
    },
    updateStartState(state, action) {
      return {
        ...state,
        startModalVisble: action.payload[0],
        startInfo: action.payload[1],
      };
    },
    updateCalibrationMtx(state, action) {
      return {
        ...state,
        calibrationError: action.payload[0],
        calibrationMtx: action.payload[1],
        calibrationOffsetUsed: action.payload[2],
        calibarionRobotUsed: action.payload[3],
      };
    },
    clearTransMtxInfo(state, action) {
      return {
        ...state,
        calibrationError: [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })], //标定的误差信息
        calibrationMtx: [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })], // 标定的矩阵
        calibrationOffsetUsed: [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })], // 标定的时候使用的激光偏移值
        calibarionRobotUsed: [
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
          [formatMessage({ id: 'app.tools.uncalibrated' }), formatMessage({ id: 'app.tools.uncalibrated' })],
        ], // 标定的时候采用的裁切机坐标
      };
    },
  },
};
